package com.cj.user.service.demo;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author cheng jun
 * @Description:
 * @Date 2021/7/18 21:34
 */
public class Demo10 {

    public Integer age = 11;
    public String name = "111";

    public void demo(int i) {
        synchronized (this.age) {
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(simpleDateFormat.format(new Date()) + ",i = " + i);
        }
    }

    public void demo2(int i) {
        //synchronized (this) {
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(simpleDateFormat.format(new Date()) + ",i = " + i);
        //}
    }

    public static void main(String[] args) {
        Demo10 demo10 = new Demo10();
        Demo10 demo11 = new Demo10();
        Demo10 demo12 = new Demo10();
        new Thread(() -> {
            demo10.demo(1);
        }).start();
        new Thread(() -> {
            demo10.demo(2);
        }).start();
        new Thread(() -> {
            demo11.demo(3);
        }).start();
        new Thread(() -> {
            demo12.demo(4);
        }).start();
    }
}
